package de.hwr.sudokuspielehr.io;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hwr.sudokuspielehr.exception.UnknownSyntaxException;

/**
 * A general file parser, used to parse content.
 * 
 * @author J. Reischauer
 * 
 */
public final class Parser {
	/**
	 * Generates a Map from the given List of Strings, containing all key-value
	 * pairs found.
	 * 
	 * @param pLines
	 *            the given content to parse as a List of Strings
	 * @return the Map containing all the key-value pairs found
	 * @throws UnknownSyntaxException
	 */
	public static Map<String, String> parse(List<String> pLines) throws UnknownSyntaxException {
		Map<String, String> result = new HashMap<String, String>();
		String[] currentKeyValuePair;

		for (String s : pLines) {
			if (!s.startsWith("#")) {
				currentKeyValuePair = s.split(":");
				if (currentKeyValuePair.length == 2) {
					result.put(currentKeyValuePair[0], currentKeyValuePair[1]);
				} else {
					throw new UnknownSyntaxException(s);
				}
			}
		}

		return result;
	}
}
